package com.achao.usercenter.common;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回类
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BaseResp<T> extends HashMap<String, Object> implements Serializable {

    /**
     * 状态码
     */
    private int code;

    /**
     * 数据
     */
    private T data;

    /**
     * 消息
     */
    private String message;


    /**
     * 扩展字段
     */
    private Map<String, String> map;

    public BaseResp() {
    }

    public BaseResp(int code, T data, String message, Map<String, Object> map) {
        super.put(BaseCommon.CODE, code);
        super.put(BaseCommon.DATA, data);
        super.put(BaseCommon.MESSAGE, message);
        if (map != null && !map.isEmpty()) {
            map.keySet().forEach(item -> super.put(item, map.get(item)));
        }
    }


    public BaseResp(int code, T data, String message) {
        this(code, data, message, new HashMap<>());
    }

    public BaseResp(int code, T data) {
        this(code, data, "", new HashMap<>());
    }

    public BaseResp(ErrorCode errorCode) {
        this(errorCode.getCode(), null, errorCode.getMessage(), new HashMap<>());
    }
}
